package com.ax.base.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.dao.BaseOrgMapper;
import com.ax.base.common.dao.BaseSubjectMapper;
import com.ax.base.common.dao.SysDictMapper;
import com.ax.base.common.domain.BaseOrg;
import com.ax.base.common.domain.BaseSubject;
import com.ax.base.common.domain.SysDict;
import com.ax.base.common.domain.Tree;
import com.ax.base.common.service.DictionarySubjectService;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.common.utils.Query;
import com.ax.base.system.domain.MenuDO;

@Service("DictionarySubjectService")
public class DictionarySubjectServiceImpl implements DictionarySubjectService {

	@Autowired
	BaseSubjectMapper baseSubjectMapper;
	
	@Autowired
	SysDictMapper sysDictMapper;
	
	@Autowired
	BaseOrgMapper baseOrgMapper;
	
	
	/**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return List<BaseSubject>
	 */
	@Override
	public List<BaseSubject> queryBaseSubjectSer(Map<String, Object> params) {
		List<BaseSubject> baseSubjects = baseSubjectMapper.queryBaseSubjectMapper(params);
		return baseSubjects;
	}
	
	/**
	 * 医学科目(模块:医学科目)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseSubject baseSubject
	 */
	@Override
	public BaseSubject getBaseSubject(Long pId) {
		BaseSubject baseSubject = baseSubjectMapper.selectByPrimaryKey(pId);
		return baseSubject;
	}

	/**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param BaseSubject baseSubject
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBaseSubjectSer(BaseSubject baseSubject) {
		baseSubject.setFirstLetter(PinYinUtil.getFirstSpell(baseSubject.getSubjectName()));
		baseSubjectMapper.insertSelective(baseSubject);
	}

	/**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param BaseSubject baseSubject
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBaseSubjectSer(BaseSubject baseSubject) {
		baseSubjectMapper.updateByPrimaryKeySelective(baseSubject);
	}

	/**
	 * 组织机构(模块:组织机构)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseOrg baseOrg
	 */
	@Override
	public BaseOrg getBaseOrg(Long orgId) {
	    BaseOrg BaseOrg = baseOrgMapper.select(orgId);
	    String[] dz = BaseOrg.getAreaCode().split(",");
	    BaseOrg.setProvince(dz[0]);
	    BaseOrg.setCity(dz[1]);
	    BaseOrg.setVillage(dz[2]);
	    return BaseOrg;
	}

	/**
	 * 组织机构查询(模块:组织机构)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return BaseOrg baseOrg
	 */
	@Override
	public List<BaseOrg> queryBaseOrgSer(Map<String, Object> params) {
		return baseOrgMapper.queryBaseOrgMapper(params);
	}
	
	/**
	 * 组织机构新增(模块:组织机构)
	 * 
	 * @param BaseOrg baseOrg
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBaseOrgSer(BaseOrg baseOrg) {
		baseOrg.setFirstLetter(PinYinUtil.getFirstSpell(baseOrg.getOrgName()));
		if(baseOrg.getOrgTypeCode()!=null && baseOrg.getOrgTypeCode().equals("1")){
			baseOrg.setOrgTypeName("医院");	
		}else{
			baseOrg.setOrgTypeName("科室");	
		}
		baseOrgMapper.insertBaseOrgMapper(baseOrg);
	}
	
	/**
	 * 组织机构修改(模块:组织机构)
	 * 
	 * @param BaseOrg baseOrg
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBaseOrgSer(BaseOrg baseOrg) {
		baseOrgMapper.updateBaseOrgMapper(baseOrg);
	}

	/**
	 * Tree
	 * 
	 * @param Long parentOrgId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getTree(Long parentOrgId) {
		String pOrgId = parentOrgId.toString();
		// 根据roleId查询权限
		List<Tree<MenuDO>> deps = baseOrgMapper.listOrg();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (id.equals(pOrgId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * Tree
	 * 
	 * @param Long parentOrgId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getSubjectTree(Long parentSubjectId) {
		String pOrgId = parentSubjectId.toString();
		// 根据roleId查询权限
		List<Tree<MenuDO>> deps = baseSubjectMapper.listSubject();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (id.equals(pOrgId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * Tree
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getInsertTree() {
		List<Tree<MenuDO>> deps = baseSubjectMapper.listTree();
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * 医生端树形选择Tree
	 * 
	 * @param Long parentSubjectId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getSubjectOtherTree(String subjectCode) {
		String code = subjectCode;
		List<Tree<MenuDO>> deps = baseSubjectMapper.listTree();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String text = deps.get(i).getText();
			if (text.contains(code)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * 名字是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean validateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>();
	    String subjectName = pt.substring(0,pt.indexOf(","));
		String parentSubjectId = pt.substring(pt.indexOf(",") + 1);
		params.put("subjectName",subjectName);
		params.put("parentSubjectId",parentSubjectId);
		boolean validate;
		validate = baseSubjectMapper.validateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 名字是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		String[] pts = pt.split(",");
		params.put("subjectName",pts[0]);
		params.put("parentSubjectId",pts[1]);
		params.put("subjectId",pts[2]);
		boolean validate;
		validate = baseSubjectMapper.validateUpdateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 字典查询(模块:字典)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public List<SysDict> querySysDictSer(Map<String,Object> params) {
	//	Query query = new Query(params);
		List<SysDict> sysDicts = sysDictMapper.querySysDictMapper(params);
	//	int total = sysDictMapper.count(query);
	//	PageUtils pageUtil = new PageUtils(sysDicts,total);
		return sysDicts;
	}
	
	/**
	 * 字典详情跳转(修改)
	 * 
	 * @param Long id
	 *           
	 * @return SysDict sysDict
	 */
	@Override
	public SysDict getQuery(Long dictId) {
		return sysDictMapper.getQueryMapper(dictId);
	}

	/**
	 * 字典新增(模块:字典管理)
	 * 
	 * @param SysDict sysDict
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertSysDictSer(SysDict sysDict) {
		sysDict.setFirstLetter(PinYinUtil.getFirstSpell(sysDict.getDictName()));
		sysDictMapper.insertSelective(sysDict);
	}

	/**
	 * 字典修改(模块:字典管理)
	 * 
	 * @param SysDict sysDict
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateSysDictSer(SysDict sysDict) {
		sysDict.setFirstLetter(PinYinUtil.getFirstSpell(sysDict.getDictName()));
		sysDictMapper.updateByPrimaryKeySelective(sysDict);
	}

}
